package com.burton.dp.barrier;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.barriers.DistributedDoubleBarrier;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator.retry.ExponentialBackoffRetry;

public class BarrierDemo {

	static String path = "/barrier_path";

	public static void main(String[] args) throws Exception {

		for (int i = 0; i < 5; i++) {
			new Thread(new Runnable() {
				public void run() {
					try {
						CuratorFramework client = CuratorFrameworkFactory
								.builder()
								.connectString("127.0.0.1:2181")
								.sessionTimeoutMs(5000)
								.retryPolicy(
										new ExponentialBackoffRetry(1000, 3))
								.build();
						client.start();
						DistributedDoubleBarrier barrier = new DistributedDoubleBarrier(
								client, path, 5);
						Thread.sleep(Math.round(Math.random() * 3000));
						barrier.enter();
						System.out.println("启动。。。");
						Thread.sleep(Math.round(Math.random() * 3000));
						barrier.leave();
						System.out.println("推出。。。。。");
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}
			}).start();
		}
	}
}
